<template>
  <div class='container'>
    <el-dialog
  :title="pageTitle"
  :visible="dialogShow"
  @close="close"
  width="50%"
  >
 <el-form :model="form" ref="form" label-width="90px" :rules="rules">
 <el-form-item label="文章标题"  prop="title"  >
    <el-input placeholder="请输入文章标题" v-model.trim="form.title" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="文章内容" prop="articleBody" >
          <quillEditor
            v-model="form.articleBody"
            @blur="$refs.form.validateField('articleBody')"
          ></quillEditor>
        </el-form-item>
          <el-form-item label="视频地址:" prop="videoURL">
     <el-input size="small" placeholder="请输入视频地址" v-model="form.videoURL" />
    </el-form-item>
</el-form>

  <span slot="footer" class="dialog-footer">
    <el-button @click="close">取 消</el-button>
    <el-button type="primary" @click="submit">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
import { add, update, detail } from '@/api/hmmm/articles.js'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'SubjectAdd',
  props: ['dialogShow', 'pageTitle'],
  components: {
    quillEditor
  },
  data () {
    return {
      form: {
        title: '',
        articleBody: '',
        videoURL: '',
        id: null
      },
      rules: {
        title: [{ required: true, message: '学科名称不能为空', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      }

    }
  },
  methods: {
    async hanldeEditForm (id) {
      this.form.id = id
      const { data } = await detail(this.form)
      this.form.title = data.title
      this.form.articleBody = data.articleBody
      this.form.videoURL = data.videoURL
    },
    close () {
      this.$refs.form.resetFields()
      this.form.title = ''
      this.form.articleBody = ''
      this.form.videoURL = ''
      this.$emit('update:dialogShow', false)
    },
    async submit () {
      try {
        await this.$refs.form.validate()

        if (this.form.id) {
          await update(this.form)
          this.$message.success('修改成功')
        } else {
          await add(this.form)
          this.$message.success('添加成功')
        }
        this.close()
        this.$parent.initData()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang='less'>
.quill-editor {
  ::v-deep .ql-editor {
    height: 100px;
  }
}
.dialog-footer{
  margin-left:650px;
}
</style>
